<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/9/28
 * Time: 2:39 PM
 */
namespace app\frontend\modules\site\api;

use app\common\exceptions\BizException;
use app\common\models\Category;
use app\frontend\modules\site\controllers\SiteBaseController;

class CategoryController extends SiteBaseController
{
    public function getList(){
        $id = request()->id;
        $pid = request()->pid;
        $name = request()->name;
        $type = request()->type;
        $order = request()->order;//[['id','desc'],['display_order','desc']]
        $offset = request()->offset;
        $limit = request()->limit;
        $pageSize = request()->page_size;
        $list = Category::site()->select(['id','pid','name','type','thumb','description','seo_title','seo_keywords','seo_description'])
            ->where('status',Category::STATUS_NORMAL);
        if($id){
            $list = $list->where('id',$id);
        }
        if($pid){
            $list = $list->where('pid',$pid);
        }
        if(trim($name)){
            $list = $list->where('name','like','%'.$name.'%');
        }
        if($type){
            $list = $list->where('type',$type);
        }
        if($order){
            foreach ($order as $v){
                if(count($v)==2){
                    $list = $list->orderBy($v[0],$v[1]);
                }
            }
        }
        if(($offset || $limit) && $pageSize){
            throw new BizException('不能同时传page_size和offset或limit');
        }
        if($offset){
            $list = $list->offset($offset);
        }
        if($limit){
            $list = $list->limit($limit);
        }
        if($pageSize){
            $list = $list->paginate($pageSize);
        }else{
            $list = $list->get();
        }
        return $this->successJson($list);

    }
}